#include "stdafx.h"
#include "HBGlobalTools.h"

#ifdef Q_WS_WIN		//windows
#include <windows.h>
#else
#endif

QString CHBGlobalTool::TrimStr( QString &sSrcStr, const QString& sTargets )
{
	sSrcStr = TrimStrLeft(sSrcStr, sTargets);
	return TrimStrRight(sSrcStr, sTargets);
}

QString CHBGlobalTool::TrimStrLeft( QString &sSrcStr, const QString& sTargets )
{
	if( sTargets.isEmpty() || sSrcStr.isEmpty())
	{
		return sSrcStr;
	}

	int iCurBeginPos = 0, iTargetsPos = 0;
	while(true)
	{
		iTargetsPos = sSrcStr.indexOf(sTargets, iCurBeginPos);
		if (iTargetsPos == iCurBeginPos)
		{
			iCurBeginPos += sTargets.size();
		}
		else
		{
			break;
		}
	}

	sSrcStr = sSrcStr.mid(iCurBeginPos, sSrcStr.size() - iCurBeginPos);
	return sSrcStr;
}

QString CHBGlobalTool::TrimStrRight( QString &sSrcStr, const QString& sTargets )
{
	if( sTargets.isEmpty() || sSrcStr.isEmpty())
	{
		return sSrcStr;
	}

	int iTargetsSize = sTargets.size();
	int iCurBeginPos = sSrcStr.size()-1, iTargetsPos = 0;
	while(true)
	{
		iTargetsPos = sSrcStr.lastIndexOf(sTargets, iCurBeginPos);
		if (iTargetsPos + iTargetsSize - 1 == iCurBeginPos)
		{
			iCurBeginPos -= sTargets.size();
		}
		else
		{
			break;
		}
	}

	sSrcStr = sSrcStr.mid(0, iCurBeginPos+1);
	return sSrcStr;
}

vecStr CHBGlobalTool::SplitStr( const QString &str, const QString &sepStr )
{
	vecStr vecSplitStr;
	int nSep =- 1;
	QString tmp, strTemp = str;
	TrimStrLeft(strTemp, sepStr);
	TrimStrRight(strTemp, sepStr);
	while( !strTemp.isEmpty() && ( nSep = strTemp.indexOf( sepStr ) ) >  0 )
	{
		tmp = strTemp.mid( 0, nSep );
		strTemp = strTemp.mid( nSep, strTemp.size() - nSep );

		TrimStrLeft(strTemp, sepStr);
		TrimStrRight(strTemp, sepStr);
		TrimStrLeft(tmp, sepStr);
		TrimStrRight(tmp, sepStr);

		if( !tmp.isEmpty() )
		{
			vecSplitStr.push_back( tmp );
		}
	}

	TrimStrLeft(strTemp, sepStr);
	TrimStrRight(strTemp, sepStr);

	if( !strTemp.isEmpty() )
	{
		vecSplitStr.push_back( strTemp );
	}

	return vecSplitStr;
}

QString CHBGlobalTool::Int2Str( qint64 iVal )
{
	QString str;
	str.setNum(iVal);
	return str;
}

QString CHBGlobalTool::Int2Str( int iVal )
{
	QString str;
	str.setNum(iVal);
	return str;
}

HBFileSysType CHBGlobalTool::GetFileSysByFilePath(const QString& sFilePathName)
{
#ifdef Q_WS_WIN		//windows
	unsigned long dwVolumeSerialNumber, dwMaximumComponentLength, dwFileSystemFlags;
	char sFileSysName[50];
	if( GetVolumeInformation(
		sFilePathName.left(3).toLatin1().data(),
		NULL, 
		0, 
		&dwVolumeSerialNumber,
		&dwMaximumComponentLength,
		&dwFileSystemFlags,
		sFileSysName,
		50 ))
	{
		QString sTmpSysName = sFileSysName;
		sTmpSysName.toUpper();
		if ("NTFS" == sTmpSysName)
		{
			return HB_FS_NTFS;
		}
		else if ("FAT32" == sTmpSysName)
		{
			return HB_FS_FAT32;
		}
		else
		{
			return HB_FS_OTHER;
		}
	}

	return HB_FS_Failed;
#else				//linux
	return HB_FS_Failed;
#endif
}

QString CHBGlobalTool::GetFilePathName( const QString& sFilePath, const QString& sFileName )
{
	QString sTmpFilePath = sFilePath;
	CHBGlobalTool::TrimStrRight(sTmpFilePath, "\\");
	CHBGlobalTool::TrimStrRight(sTmpFilePath, "/");
	return (sTmpFilePath + "/" + sFileName);
}

bool CHBGlobalTool::QuickCreateFile( const QString& sFilePathName, qint64 iFileSize )
{
	QFile file(sFilePathName);  
	file.open(QIODevice::WriteOnly);  
	file.close(); 
	return QFile::resize(sFilePathName, iFileSize);
}

qint64 CHBGlobalTool::GetFileSize( const QString& sFilePathName )
{
	QFile file(sFilePathName);
	if (!file.open(QIODevice::ReadOnly))
		return -1;

	return file.size();
}
